# Bottle 
try:
    import bottle
    from bottle import run, route, request, HTTPError, HTTPResponse, redirect, response
except ImportError:
    "Install bottle, please."
    
import json


def check_auth(username, password):
    if username == 'admin' and password == 'pass':
        return True
    else:
        return False

def authenticate(msg_string = "Authenticate."):
    response.content_type = "application/json"
    message = {'message': msg_string}
    resp = json.dumps(message)
    response.status = "401 - Unauthorized"
    response.headers['WWW-Authenticate'] = 'Basic realm="PyBit"'
    
    return resp

def requires_plain_auth(f):
    def decorated(*args, **kwargs):
        print request.auth
        auth = request.auth
        if not auth: 
            return authenticate()
        elif not check_auth(auth[0],auth[1]):
            response.status = "401 - Unauthorized"
            return authenticate("HTTP Authentication Failed.")
        else:
            return f(*args, **kwargs)
    return decorated